package org.example.library.service;

import org.example.library.pojo.Book;
import org.example.library.pojo.BorrowRecord;
import org.example.library.pojo.User;
import org.example.library.pojo.dto.BorrowRecordDto;
import org.example.library.repository.IBookRepository;
import org.example.library.repository.IBorrowRecordRepository;
import org.example.library.repository.IUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BorrowRecordService implements IBorrowRecordService {

    @Autowired
    private IUserRepository userRepository;

    @Autowired
    private IBookRepository bookRepository;

    @Autowired
    private IBorrowRecordRepository borrowRecordRepository;

    /**
     * 添加借阅记录
     */
    @Override
    public BorrowRecord add(BorrowRecordDto borrowRecordDto) {
        // 检查用户是否存在
        User user = userRepository.findById(borrowRecordDto.getUserId())
                .orElseThrow(() -> new IllegalArgumentException("用户不存在"));

        // 检查书籍是否存在
        Book book = bookRepository.findById(borrowRecordDto.getBookId())
                .orElseThrow(() -> new IllegalArgumentException("书籍不存在"));

        // 检查库存是否足够
        if (book.getStock() <= 0) {
            throw new IllegalArgumentException("库存不足，无法借阅");
        }

        // 创建借阅记录
        BorrowRecord record = new BorrowRecord();
        record.setUser(user);
        record.setBook(book);
        record.setBorrowDate(LocalDateTime.now());
        record.setDueDate(LocalDateTime.now().plusDays(7)); // 默认应还时间为 7 天后
        record.setReturnDate(null); // 初始值为 null

        // 更新库存
        book.setStock(book.getStock() - 1);
        bookRepository.save(book);

        // 保存借阅记录
        return borrowRecordRepository.save(record);
    }

    /**
     * 更新借阅记录
     */
    @Override
    public BorrowRecord update(BorrowRecordDto borrowRecordDto) {
        // 检查借阅记录是否存在
        BorrowRecord record = borrowRecordRepository.findById(borrowRecordDto.getId())
                .orElseThrow(() -> new IllegalArgumentException("借阅记录不存在"));

        // 更新归还日期
        if (borrowRecordDto.getReturnDate() != null) {
            record.setReturnDate(borrowRecordDto.getReturnDate());
        }

        // 保存更新后的记录
        return borrowRecordRepository.save(record);
    }

    /**
     * 删除借阅记录
     */
    @Override
    public void delete(Integer recordId) {
        // 检查借阅记录是否存在
        BorrowRecord record = borrowRecordRepository.findById(recordId)
                .orElseThrow(() -> new IllegalArgumentException("借阅记录不存在"));

        // 如果删除借阅记录，恢复库存
        Book book = record.getBook();
        book.setStock(book.getStock() + 1);
        bookRepository.save(book);

        // 删除借阅记录
        borrowRecordRepository.delete(record);
    }

    /**
     * 根据 ID 查询借阅记录
     */
    @Override
    public BorrowRecord getRecordById(Integer recordId) {
        return borrowRecordRepository.findById(recordId)
                .orElseThrow(() -> new IllegalArgumentException("借阅记录不存在"));
    }

    @Override
    public List<BorrowRecord> getAllRecords() {
        return borrowRecordRepository.findAll();
    }

    /**
     * 还书操作
     */
    @Override
    public BorrowRecord returnBook(Integer recordId) {
        // 检查借阅记录是否存在
        BorrowRecord record = borrowRecordRepository.findById(recordId)
                .orElseThrow(() -> new IllegalArgumentException("借阅记录不存在"));

        // 检查是否已经归还
        if (record.getReturnDate() != null) {
            throw new IllegalArgumentException("该书籍已归还");
        }

        // 设置归还日期为当前时间
        record.setReturnDate(LocalDateTime.now());

        // 更新库存
        Book book = record.getBook();
        book.setStock(book.getStock() + 1);
        bookRepository.save(book);

        // 保存更新后的借阅记录
        return borrowRecordRepository.save(record);
    }
}